package com.laogu1;

import com.lagou.util.ZkUtil;
import com.laogu1.service.UserServiceImpl;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServerBoot {

    private static ZkClient zkClient = new ZkClient(ZkUtil.ZK_ADDRESS);

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ServerBoot.class, args);
        UserServiceImpl.startServer("127.0.0.1", 7999);

        updateCostTime();
    }

    private static void updateCostTime() throws Exception {
        while (true) {
            Thread.sleep(1000);
            Object readData = zkClient.readData(ZkUtil.PATH + "/127.0.0.1_7999");
            String[] split = readData.toString().split("_");
            if (System.currentTimeMillis() - Long.valueOf(split[0]) > 10000) {
                UserServiceImpl.registryZk("127.0.0.1", 7999);
                System.out.println("服务端10s中未收到请求，空闲状态，重置请求时间");
            }
        }
    }
}
